package com.probox.common.entity.service.centre.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import com.probox.common.entity.domain.centre.OrderInfo;
import com.probox.common.entity.mapper.centre.CentreOrderInfoMapper;
import com.probox.common.entity.service.centre.IOrderInfoService;
import com.probox.common.entity.vo.centre.WarningDataVo;
import com.probox.common.entity.vo.centre.customer.CustomerQueryVo;
import com.probox.common.entity.vo.centre.map.Legend;
import com.probox.common.entity.vo.centre.map.ProductAxisMap;
import com.probox.common.entity.vo.centre.map.Series;
import com.probox.common.entity.vo.centre.map.XAxis;
import com.probox.common.entity.vo.centre.order.CompareDataVo;
import com.probox.common.entity.vo.centre.order.OrderDataVo;
import com.probox.common.entity.vo.centre.order.OrderQueryVo;
import com.probox.common.core.enums.*;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.money.MoneyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import static cn.hutool.core.date.DateUnit.HOUR;

/**
 * 订单子Service业务层处理
 *
 * @author hds
 * @date 2021-06-24
 */

@Service
@Transactional
public class CentreOrderInfoServiceImpl extends ServiceImpl<CentreOrderInfoMapper, OrderInfo> implements IOrderInfoService {
    @Autowired
    private CentreOrderInfoMapper centreOrderInfoMapper;


    /**
     * 总单数  总金额 交易单数 交易金额
     * @param orderQueryVo
     * @return
     */
    @Override
    public OrderDataVo getOrderCollect(OrderQueryVo orderQueryVo) throws Exception {
        OrderDataVo vo = centreOrderInfoMapper.selectOrderCollect(orderQueryVo);
        OrderDataVo orderDataVo = centreOrderInfoMapper.selectMakeOrderCollect(orderQueryVo);
        vo.setMakeOrderMoneyTotal(orderDataVo.getMakeOrderMoneyTotal());
        vo.setMakeOrderTotal(orderDataVo.getMakeOrderTotal());
        return vo;
    }

    @Override
    public BigDecimal sumPayAmount(OrderQueryVo queryVo) {
        return centreOrderInfoMapper.sumPayAmount(queryVo);
    }

    /**
     * 取消单数、取消金额、未支付单数、未支付金额
     * @param orderQueryVo
     * @return
     */
    @Override
    public OrderDataVo getOrderUnfinishedCollect(OrderQueryVo orderQueryVo) {
        OrderDataVo vo = centreOrderInfoMapper.selectCancelOrderCollect(orderQueryVo);
        OrderDataVo orderDataVo = centreOrderInfoMapper.selectNonPaymentCollect(orderQueryVo);
        vo.setNonPaymentOrderMoneyTotal(orderDataVo.getNonPaymentOrderMoneyTotal());
        vo.setNonPaymentOrderTotal(orderDataVo.getNonPaymentOrderTotal());
        return vo;
    }

    /**
     * 售后单数、投诉单数、买家数量、成交卖家数、取消卖家数
     * @param orderQueryVo
     * @return
     */
    @Override
    public OrderDataVo getOrderAfterSaleCollect(OrderQueryVo orderQueryVo) {
        OrderDataVo orderDataVo = centreOrderInfoMapper.selectOrderAfterSaleCollect(orderQueryVo);
        orderDataVo.setClientTotal(centreOrderInfoMapper.SelectClientTotal(orderQueryVo));
        orderDataVo.setMakeClientTotal(centreOrderInfoMapper.selectMakeClientTotal(orderQueryVo));
        orderDataVo.setCancelClientTotal(centreOrderInfoMapper.selectCancelClientTotal(orderQueryVo));
        return orderDataVo;
    }

    /**
     * 查询 支付金额  支付买家数
     * @param orderQueryVo
     * @return
     */
    @Override
    public OrderDataVo getOrderTodayCollect(OrderQueryVo orderQueryVo) {
        OrderDataVo orderDataVo = centreOrderInfoMapper.selectMakeOrderCollect(orderQueryVo);
        orderDataVo.setMakeClientTotal(centreOrderInfoMapper.selectMakeClientTotal(orderQueryVo));
        return orderDataVo;
    }

    /**
     * 查询 今天、昨日全天支付金额图表数据
     * @param orderQueryVo
     * @return
     */
    @Override
    public ProductAxisMap getOrderTodayMap(OrderQueryVo orderQueryVo) {
        DateTime beginOfDay = DateUtil.beginOfDay(DateUtil.date());
        DateTime endOfDay = DateUtil.endOfDay(DateUtil.date());
        orderQueryVo.setStartTime(beginOfDay);
        orderQueryVo.setEndTime(endOfDay);
        List<HashMap> hashMaps = centreOrderInfoMapper.selectOrderTodayData(orderQueryVo);
        List date = getDate(beginOfDay, endOfDay);
        //金额数据
        List list = seriesData(hashMaps, date);
        List dateEqual = getDateEqual(beginOfDay, endOfDay);
        XAxis xAxis = new XAxis();
        xAxis.setData(dateEqual);

        List seriesList = new ArrayList();
        Series series = buildSeriesMap(list, CentreOrderType.todayOrderDate.getKey());
        seriesList.add(series);

        //昨天数据
        beginOfDay = DateUtil.beginOfDay(DateUtil.yesterday());
        endOfDay = DateUtil.endOfDay(DateUtil.yesterday());
        orderQueryVo.setStartTime(beginOfDay);
        orderQueryVo.setEndTime(endOfDay);
        hashMaps = centreOrderInfoMapper.selectOrderTodayData(orderQueryVo);
        //金额数据
        date = getDate(beginOfDay, endOfDay);
         list = seriesData(hashMaps, date);
         series = buildSeriesMap(list, CentreOrderType.yesterdayOrderDate.getKey());
        seriesList.add(series);

        Legend legend = buildLegendMap(seriesList);

        return  buildProductAxisMap( CentreOrderType.todayOrderDate.getKey(),seriesList,xAxis,legend);
    }

    /**
     * 总金额 总单数 成交金额 成交单数 与前相同天数进行对比数据
     * @param orderQueryVo
     * @return
     */
    @Override
    public OrderDataVo getOrderMoneyCollect(OrderQueryVo orderQueryVo) throws Exception {
        OrderDataVo orderCollect = this.getOrderCollect(orderQueryVo);
        OrderDataVo orderDataVo = new OrderDataVo();

        Date startTime = orderQueryVo.getStartTime();
        Date endTime = orderQueryVo.getEndTime();
        if (startTime != null && endTime != null){
            //获取时间差
            Integer betweenDay = Integer.parseInt(DateUtil.between(startTime, endTime, DateUnit.DAY)+"")*-1;
            //往前推 7 、30 、
            Date startTimesFormerly = DateUtil.offsetDay(startTime, betweenDay);
            Date endTimesFormerly = DateUtil.offsetDay(endTime, betweenDay);
            orderQueryVo.setStartTime(startTimesFormerly);
            orderQueryVo.setEndTime(endTimesFormerly);
            orderDataVo  = this.getOrderCollect(orderQueryVo);
        }
        CompareDataVo vo = countFormerly(orderCollect, orderDataVo);
        return vo;
    }

    /**
     * 查询订单总额数据汇总
     * @param orderQueryVo
     * @return
     */
    @Override
    public ProductAxisMap getOrderMoneyTotalMap(OrderQueryVo orderQueryVo) {
        Date startTime = new Date();
        Date endTime = DateUtil.lastMonth();
        startTime = DateUtil.endOfDay(startTime);
        endTime = DateUtil.beginOfDay(endTime);
        orderQueryVo.setStartTime(endTime);
        orderQueryVo.setEndTime(startTime);

        //生成近三十天日期
        List<String> date = getOrderDate(DateUtil.offsetDay(startTime, -1), endTime);
        List<HashMap> hashMaps = centreOrderInfoMapper.selectOrderMoneyTotalMap(orderQueryVo);
        //金额数据
        List list = seriesData(hashMaps, date);
        XAxis xAxis = new XAxis();
        xAxis.setData(date);

        List seriesList = new ArrayList();
        Series series = buildSeriesMap(list, orderQueryVo.getType());
        seriesList.add(series);

        Legend legend = buildLegendMap(seriesList);
       return buildProductAxisMap( orderQueryVo.getType(),seriesList,xAxis,legend);
    }


    /**
     * 查询总单数
     * @param orderQueryVo
     * @return
     */
    @Override
    public ProductAxisMap getOrderTotalMap(OrderQueryVo orderQueryVo) {
        Date startTime = new Date();
        Date endTime = DateUtil.lastMonth();

        //生成近三十天日期
        List<String> date = getOrderDate(DateUtil.offsetDay(startTime, -1), endTime);
        List<HashMap> hashMaps = centreOrderInfoMapper.selectOrderTotalMap(orderQueryVo);
        //金额数据
        List list = seriesData(hashMaps, date);
        XAxis xAxis = new XAxis();
        xAxis.setData(date);

        List seriesList = new ArrayList();
        Series series = buildSeriesMap(list, orderQueryVo.getType());
        seriesList.add(series);

        Legend legend = buildLegendMap(seriesList);
        return buildProductAxisMap( orderQueryVo.getType(),seriesList,xAxis,legend);
    }

    /**
     * 查询订单成交金额
     * @param orderQueryVo
     * @return
     */
    @Override
    public ProductAxisMap getMakeOrderMoneyTotalMap(OrderQueryVo orderQueryVo) {
        Date startTime = new Date();
        Date endTime = DateUtil.lastMonth();

        //生成近三十天日期
        List<String> date = getOrderDate(DateUtil.offsetDay(startTime, -1), endTime);
        List<HashMap> hashMaps = centreOrderInfoMapper.selectMakeOrderMoneyTotalMap(orderQueryVo);
        //金额数据
        List list = seriesData(hashMaps, date);
        XAxis xAxis = new XAxis();
        xAxis.setData(date);

        List seriesList = new ArrayList();
        Series series = buildSeriesMap(list, orderQueryVo.getType());
        seriesList.add(series);

        Legend legend = buildLegendMap(seriesList);
        return buildProductAxisMap( orderQueryVo.getType(),seriesList,xAxis,legend);
    }


    /**
     * 查询成交单数
     * @param orderQueryVo
     * @return
     */
    @Override
    public ProductAxisMap getMakeOrderTotalMap(OrderQueryVo orderQueryVo) {
        Date startTime = new Date();
        Date endTime = DateUtil.lastMonth();

        //生成近三十天日期
        List<String> date = getOrderDate(DateUtil.offsetDay(startTime, -1), endTime);
        List<HashMap> hashMaps = centreOrderInfoMapper.selectMakeOrderTotalMap(orderQueryVo);
        //金额数据
        List list = seriesData(hashMaps, date);
        XAxis xAxis = new XAxis();
        xAxis.setData(date);

        List seriesList = new ArrayList();
        Series series = buildSeriesMap(list, orderQueryVo.getType());
        seriesList.add(series);

        Legend legend = buildLegendMap(seriesList);
        return buildProductAxisMap( orderQueryVo.getType(),seriesList,xAxis,legend);
    }

    /**
     * 查询取消订单金额
     * @param orderQueryVo
     * @return
     */
    @Override
    public ProductAxisMap getCancelMoney(OrderQueryVo orderQueryVo) {
        Date startTime = new Date();
        Date endTime = DateUtil.lastMonth();

        //生成近三十天日期
        List<String> date = getOrderDate(DateUtil.offsetDay(startTime, -1), endTime);
        List<HashMap> hashMaps = centreOrderInfoMapper.selectCancelMoney(orderQueryVo);
        //金额数据
        List list = seriesData(hashMaps, date);
        XAxis xAxis = new XAxis();
        xAxis.setData(date);

        List seriesList = new ArrayList();
        Series series = buildSeriesMap(list, orderQueryVo.getType());
        seriesList.add(series);

        Legend legend = buildLegendMap(seriesList);
        return buildProductAxisMap( orderQueryVo.getType(),seriesList,xAxis,legend);
    }

    /**
     * 查询预警数据
     * @param orderQueryVo
     * @return
     */
    @Override
    public WarningDataVo getWarningData(OrderQueryVo orderQueryVo) {
        Long platformId = SecurityUtils.getPlatformId();

        WarningDataVo vo = new WarningDataVo();
        QueryWrapper<OrderInfo> orderInfoQueryWrapper = new QueryWrapper<OrderInfo>();
        orderInfoQueryWrapper.eq("ORDER_STATUS", DataStatus.valid.getKey());
        orderInfoQueryWrapper.eq("PLATFORM_UID",platformId);
        orderInfoQueryWrapper.eq("PAY_STATUS", PayStatusEnum.paid.getKey());
        orderInfoQueryWrapper.eq("SEND_STATUS", SendStatusEnum.unShip.getKey());
        if (orderQueryVo.getStartTime()!=null){
            orderInfoQueryWrapper.ge("CREATE_TIME",orderQueryVo.getStartTime());
        }
        if (orderQueryVo.getEndTime()!=null){
            orderInfoQueryWrapper.le("CREATE_TIME",orderQueryVo.getEndTime());
        }
        //待发货数量
        Integer sendTotal = centreOrderInfoMapper.selectCount(orderInfoQueryWrapper);
        vo.setSendTotal(sendTotal);

        //待售后订单数量
        Integer saleTotal = centreOrderInfoMapper.selectSaleTotal(orderQueryVo);
        vo.setSaleTotal(saleTotal);

        //已发货数量
        orderInfoQueryWrapper =  new QueryWrapper<OrderInfo>();
        if (orderQueryVo.getStartTime()!=null){
            orderInfoQueryWrapper.ge("CREATE_TIME",orderQueryVo.getStartTime());
        }
        if (orderQueryVo.getEndTime()!=null){
            orderInfoQueryWrapper.le("CREATE_TIME",orderQueryVo.getEndTime());
        }
        orderInfoQueryWrapper.eq("SEND_STATUS",SendStatusEnum.ship.getKey());
        orderInfoQueryWrapper.eq("ORDER_STATUS", DataStatus.valid.getKey());
        orderInfoQueryWrapper.eq("PLATFORM_UID",platformId);
        orderInfoQueryWrapper.eq("PAY_STATUS", PayStatusEnum.paid.getKey());
        Integer shipped = centreOrderInfoMapper.selectCount(orderInfoQueryWrapper);
        vo.setShipped(shipped);

        // 待付款单数
        orderInfoQueryWrapper =  new QueryWrapper<OrderInfo>();
        if (orderQueryVo.getStartTime()!=null){
            orderInfoQueryWrapper.ge("CREATE_TIME",orderQueryVo.getStartTime());
        }
        if (orderQueryVo.getEndTime()!=null){
            orderInfoQueryWrapper.le("CREATE_TIME",orderQueryVo.getEndTime());
        }
        orderInfoQueryWrapper.eq("ORDER_STATUS", DataStatus.valid.getKey());
        orderInfoQueryWrapper.eq("PLATFORM_UID",platformId);
        orderInfoQueryWrapper.eq("PAY_STATUS", PayStatusEnum.unpaid.getKey());
        Integer stayPayOrder = centreOrderInfoMapper.selectCount(orderInfoQueryWrapper);
        vo.setStayPayOrder(stayPayOrder);

        return vo;
    }

    /**
     * 成交客户数
     * @return
     */
    @Override
    public Integer selectMakeClientTotal(CustomerQueryVo customerQueryVo){
        OrderQueryVo orderQueryVo = new OrderQueryVo();
        if (customerQueryVo.getStartTime()!=null){
            orderQueryVo.setStartTime(DateUtil.beginOfDay(customerQueryVo.getStartTime()));
        }
        if (customerQueryVo.getEndTime()!=null){
            orderQueryVo.setEndTime(DateUtil.endOfDay(customerQueryVo.getEndTime()));
        }

        return centreOrderInfoMapper.selectMakeClientTotal(orderQueryVo);
    }




    public Integer selectMakeClientTotal(){
        OrderQueryVo orderQueryVo = new OrderQueryVo();
        orderQueryVo.setStartTime(DateUtil.beginOfDay(DateUtil.date()));
        orderQueryVo.setEndTime(DateUtil.endOfDay(DateUtil.date()));
        return centreOrderInfoMapper.selectMakeClientTotal(orderQueryVo);

    }


    /**
     * 当前时间段数据：过去时间段数据比较
     * @return
     */
    public CompareDataVo countFormerly(OrderDataVo compareDataVo , OrderDataVo orderDataVoFormerly ) throws Exception {
        CompareDataVo vo = new CompareDataVo();
        vo.setMakeOrderMoneyTotal(compareDataVo.getMakeOrderMoneyTotal());
        vo.setMakeOrderTotal(compareDataVo.getMakeOrderTotal());
        vo.setOrderMoneyTotal(compareDataVo.getOrderMoneyTotal());
        vo.setOrderTotal(compareDataVo.getOrderTotal());

        if (orderDataVoFormerly.getMakeOrderTotal()!=null){
            vo.setMakeOrderMoneyTotalFormerly(difference(compareDataVo.getMakeOrderMoneyTotal(),orderDataVoFormerly.getMakeOrderMoneyTotal(), 2));
            vo.setMakeOrderTotalFormerly(difference(compareDataVo.getMakeOrderTotal(),orderDataVoFormerly.getMakeOrderTotal(), 2));
            vo.setOrderMoneyTotalFormerly(difference(compareDataVo.getOrderMoneyTotal(), orderDataVoFormerly.getOrderMoneyTotal(), 2));
            vo.setOrderTotalFormerly(difference(compareDataVo.getOrderTotal(),orderDataVoFormerly.getOrderTotal(), 2));
        }
        return vo;
    };


    /**
     * 构建buildSeries对象
     * @param collect
     * @param type
     * @return
     */
    public Series buildSeriesMap(List<Double> collect, String type){
        Series series = new Series();
        series.setData(collect);
        series.setType("line");
//        series.setStack("总量");
        if (CentreOrderType.todayOrderDate.getKey().equals(type)){
            series.setName(CentreOrderType.todayOrderDate.getValue());
            series.setColor("#00DB00");
        }else if (CentreOrderType.makeOrderMoneyTotal.getKey().equals(type)){
            series.setName(CentreOrderType.makeOrderMoneyTotal.getValue());
            series.setColor("#FF0000");
        }else if (CentreOrderType.makeOrderTotal.getKey().equals(type)){
            series.setName(CentreOrderType.makeOrderTotal.getValue());
            series.setColor("#F7BE81");
        }else if (CentreOrderType.orderTotal.getKey().equals(type)){
            series.setName(CentreOrderType.orderTotal.getValue());
            series.setColor("#F7BE81");
        }else if (CentreOrderType.orderMoneyTotal.getKey().equals(type)) {
            series.setName(CentreOrderType.orderMoneyTotal.getValue());
            series.setColor("#FF0000");
        }else if (CentreOrderType.yesterdayOrderDate.getKey().equals(type)) {
            series.setName(CentreOrderType.yesterdayOrderDate.getValue());
            series.setColor("#0072E3");
        }else if (CentreOrderType.cancelOrderDate.getKey().equals(type)) {
            series.setName(CentreOrderType.cancelOrderDate.getValue());
        }
        return series;
    }

    /**
     * 构建buildLegendMap对象
     * @return
     */
    public Legend buildLegendMap(List<Series> list){
        Legend legend = new Legend();
        List datalist = new ArrayList<>();
        for (Series i : list){
            datalist.add(i.getName());
        }
        legend.setData(datalist);
        return legend;
    }

    /**
     * 构建 ProductAxisMap 对象
     * @return
     */
    public ProductAxisMap  buildProductAxisMap(String type, List<Series> seriesList , XAxis xAxis, Legend legend){
        ProductAxisMap productAxisMap = new ProductAxisMap();
        if (CentreOrderType.todayOrderDate.getKey().equals(type)){
            productAxisMap.setTitle(CentreOrderType.todayOrderDate.getValue());
        }else if (CentreOrderType.makeOrderMoneyTotal.getKey().equals(type)){
            productAxisMap.setTitle(CentreOrderType.makeOrderMoneyTotal.getValue());
        }else if (CentreOrderType.makeOrderTotal.getKey().equals(type)){
            productAxisMap.setTitle(CentreOrderType.makeOrderTotal.getValue());
        }else if (CentreOrderType.orderTotal.getKey().equals(type)){
            productAxisMap.setTitle(CentreOrderType.orderTotal.getValue());
        }else if (CentreOrderType.orderMoneyTotal.getKey().equals(type)) {
            productAxisMap.setTitle(CentreOrderType.orderMoneyTotal.getValue());
        }else if (CentreOrderType.cancelOrderDate.getKey().equals(type)) {
            productAxisMap.setTitle(CentreOrderType.cancelOrderDate.getValue());
        }

        productAxisMap.setXAxis(xAxis);
        productAxisMap.setSeries(seriesList);
        productAxisMap.setLegend(legend);
        return productAxisMap;
    }

    /**
     * 构建 seriesData 下的数据
     * @param list
     * @param date
     * @return
     */
    public List<Double> seriesData(List<HashMap> list , List<String> date){

        List<Double> collect = date.stream().map(o -> {
            Double total = 0.00;
            HashMap hashMap = new HashMap();
            hashMap = list.stream().filter(i -> i.get("date").equals(o)).findAny().orElse(null);
            if (hashMap!=null){
                try {
                    total =  Double.valueOf(hashMap.get("total").toString())  ;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return total;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 构建 seriesData 下的数据
     * @param list
     * @param date
     * @return
     */
    public List<Double> seriesData(List<HashMap> list , List<String> date, Integer percentage){
        List<Double> collect = date.stream().map(o -> {
            Double total = 0.00;
            HashMap hashMap = new HashMap();
            hashMap = list.stream().filter(i -> i.get("date").equals(o)).findAny().orElse(null);
            if (hashMap!=null){
                total = mul( Double.valueOf(hashMap.get("total").toString()),percentage);
            }
            return total;
        }).collect(Collectors.toList());
        return collect;
    }


    /**
     * 获取24小时日期集合
     * @param startTime
     * @param endTime
     * @return
     */
    public static List getDate(Date startTime, Date endTime){
        List list = new ArrayList<>();
        Long betweenDay = DateUtil.between(startTime, endTime,HOUR);
        endTime = DateUtil.offsetDay(endTime, -1);
        for (int i = 0; i <24 ; i++) {
            endTime = DateUtil.offsetHour(endTime, 1);
            String format = DateUtil.format(endTime, "yyyy-MM-dd:H");
            list.add(format);
            System.out.println(format);
        }
        return list;
    }

    /**
     * 获取某段时间内的日期集合
     * @param startTime
     * @param endTime
     * @return
     */
    public static List getOrderDate(Date startTime, Date endTime){
        List list = new ArrayList<>();
        long betweenDay = DateUtil.between(startTime, endTime, DateUnit.DAY);
        for (int i = 0; i <betweenDay ; i++) {
            endTime = DateUtil.offsetDay(endTime, 1);
            String format = DateUtil.format(endTime, "yyyy-MM-dd");
            list.add(format);
        }
        return list;
    }
    /**
     * * 获取24小时日期集合
     * @param startTime
     * @param endTime
     * @return
             */
    public static List getDateEqual(Date startTime, Date endTime){
        List list = new ArrayList<>();
        Long betweenDay = DateUtil.between(startTime, endTime,HOUR);
        Integer a = 0;
        for (int i = 0; i <24 ; i++) {
            endTime = DateUtil.offsetHour(endTime, 1);
            String format = DateUtil.format(endTime, "H");
            Integer hour = Integer.parseInt(format);
            a += 1;
            if (a>3){
                list.add(format);
                a = 1;
            }else{
                list.add("");
            }
        }
        return list;
    }

    /**
     * 提供精确的乘法运算
     *
     * @param v1 被乘数
     * @param v2 乘数
     * @return 两个参数的积
     */
    public static double mul(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2).doubleValue();
    }
    /**
     * 提供（相对）精确的除法运算。当发生除不尽的情况时，由scale参数指
     * 定精度，以后的数字四舍五入
     *
     * @param v1    被除数
     * @param v2    除数
     * @param scale 表示需要精确到小数点以后几位
     * @return 两个参数的商
     */
    public static String div(Integer v1, Integer v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        if (v2==0){
            return v1.toString();
        }
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     * 提供（相对）精确的除法运算。当发生除不尽的情况时，由scale参数指
     * 定精度，以后的数字四舍五入
     *
     * @param v1    被除数
     * @param v2    除数
     * @param scale 表示需要精确到小数点以后几位
     * @return 两个参数的商
     */
    public static Double div(Double v1, Double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        if (v2==0.00){
            return v1;
        }
        return Double.valueOf(b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).toString());
    }

    /**
     * 两个数字之间的差值百分比
     * @param v1    被除数
     * @param v2    除数
     * @param scale 表示需要精确到小数点以后几位
     * @return 两个参数的商
     */
    public static String difference(BigDecimal v1, BigDecimal v2, Integer scale) {
        if (v2.compareTo(BigDecimal.ZERO)  == 0){
            return "- -";
        }
        BigDecimal divide = v1.divide(v2, scale, BigDecimal.ROUND_HALF_UP);
        String total = MoneyUtils.changeY2F(divide.toString());
        if("0".equals(total)){
            return "- -";
        }
        return total+"%";
    }

    public static String difference(Integer v1, Integer v2, Integer scale) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);

        if (b2.compareTo(BigDecimal.ZERO)  == 0){
            return "- -";
        }
        BigDecimal divide = b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP);
//        String total = divide.multiply(new BigDecimal(100)).toString();
        String total = MoneyUtils.changeY2F(divide.toString());
        if("0".equals(total)){
            return "- -";
        }
        return total+"%";
    }

}
